package travelDream.beans;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.validation.constraints.Pattern;

import org.primefaces.event.ToggleEvent;

import traveldream.ejb.DTO.BookingDTO;
import traveldream.ejb.DTO.ExcursionDTO;
import traveldream.ejb.DTO.HotelDTO;
import traveldream.ejb.DTO.PackageDTO;
import traveldream.ejb.DTO.TransportationDTO;
import traveldream.ejb.interfaces.ExcursionBeanLocal;
import traveldream.ejb.interfaces.GiftListBeanLocal;
import traveldream.ejb.interfaces.HotelBeanLocal;
import traveldream.ejb.interfaces.NotFindException;
import traveldream.ejb.interfaces.PackagePurchaseBeanLocal;
import traveldream.ejb.interfaces.TransportationBeanLocal;

@ManagedBean(name = "PackagePM")
@SessionScoped
public class PackagePurchaseManaged {

	private PackageDTO pack;
	private BookingDTO bDTO;
	private ExcursionDTO eDTO;
	private boolean customize;
	private String link;
	
	@EJB
	PackagePurchaseBeanLocal PackagePurchaseBeanLocal;
	@EJB
	private HotelBeanLocal HotelBeanLocal;
	@EJB
	private TransportationBeanLocal TransportationBeanLocal;
	@EJB
	private ExcursionBeanLocal ExcursionBeanLocal;
	@EJB
	private GiftListBeanLocal GiftListBeanLocal;
	
	public PackagePurchaseManaged(){
		bDTO=new BookingDTO();
	}
	public String buy2(PackageDTO pack,HotelDTO hDTO, TransportationDTO gDTO, TransportationDTO rDTO, ExcursionDTO eDTO){
		if(pack!=null){
			this.bDTO.sethDTO(hDTO);
			this.bDTO.setgDTO(gDTO);
			this.bDTO.setrDTO(rDTO);
			this.eDTO = eDTO;
			this.pack = pack;
			customize = false;
		}
		if(bDTO.getnOfPeople()<1){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "You have to buy at least 1 package!!"));
			return "";}
		if(PackagePurchaseBeanLocal.checkAvailability(bDTO,eDTO)){
			link = "http://localhost:8080/travelDreamWeb/joinPackage.xhtml?id="+pack.getId()+"&c=0";
			return "packagePurchase.xhtml?faces-redirect=true";
			}
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "No places available for "+ bDTO.getnOfPeople() + " people!!. Try with less ones"));
		return "";
	}
	
	public String buycustomize(PackageDTO pDTO){
			if(pDTO!=null){
				try{
				this.bDTO.sethDTO(HotelBeanLocal.find(pDTO.getIdHotel()));
				this.bDTO.setgDTO(TransportationBeanLocal.find(pDTO.getIdGoing()));
				this.bDTO.setrDTO(TransportationBeanLocal.find(pDTO.getIdReturn()));
				}
				catch(NotFindException e){
					FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please, go back and try again!!"));
				}
				try{
				this.eDTO = ExcursionBeanLocal.find(pDTO.getIdExcursion());
				}
				catch(NotFindException e){
					if(pDTO.getIdExcursion()>0)
						FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Please try again!!"));
				}
				this.pack = pDTO;
				customize = true;
		}else{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "You can't customize this Package!"));
			return "";
		}
		link = "http://localhost:8080/travelDreamWeb/joinPackage.xhtml?id="+pDTO.getId()+"&c=1";
		return "packagePurchase.xhtml?faces-redirect=true";
	}
	
	public String createGiftList(){
		if(bDTO.getnOfPeople()<1){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "You have to buy at least 1 packge!!"));
			return "";
		}
		try
		{
			GiftListBeanLocal.createGiftList(pack,bDTO.getnOfPeople());
		}
		catch(Exception e){
			return "package.xhtml?faces-redirect=true&error=alreadyBought";
		}
			return "home.xhtml?faces-redirect=true";
	}
	
	public String purchase(){
	try
	{
		if(customize){
			PackagePurchaseBeanLocal.buyCustomize(pack);
		}else{
			System.out.println("Default Package");
			PackagePurchaseBeanLocal.buy(pack);
		}
	}
	catch(NotFindException e){
		return "package.xhtml?faces-redirect=true&error=nullPointer";
	}
	catch(Exception e){
		return "package.xhtml?faces-redirect=true&error=alreadyBought";
	}
		PackagePurchaseBeanLocal.booking(bDTO, eDTO);
		return "home.xhtml?faces-redirect=true&p=ok";
	}

	public PackageDTO getPack() {
		return pack;
	}

	public void setPack(PackageDTO pack) {
		this.pack = pack;
	}



	public ExcursionDTO geteDTO() {
		return eDTO;
	}

	public void seteDTO(ExcursionDTO eDTO) {
		this.eDTO = eDTO;
	}

	public BookingDTO getbDTO() {
		return bDTO;
	}

	public void setbDTO(BookingDTO bDTO) {
		this.bDTO = bDTO;
	}

	public String getLink() {
		return link;
	}
	public void setLink(String link) {
		this.link = link;
	}
	
}
